#!/bin/bash

# 定义源文件路径
SOURCE_FILE="./pcs.elf"

# 定义目标设备的用户名和IP地址
REMOTE_USER="ubuntu"
REMOTE_HOST="192.168.1.10"

# 定义目标路径
REMOTE_PATH="/home/ubuntu"

# 定义SSH密码
SSH_PASSWORD="temppwd"

# 定义要在远程系统上执行的命令
REMOTE_COMMAND="gdbserver :9001 /home/ubuntu/pcs.elf"

# 定义要检查的进程名称或特定标识
PROCESS_NAME="gdbserver"

# 检查源文件是否存在
if [ ! -f "$SOURCE_FILE" ]; then
    echo "Error: Source file does not exist."
    exit 1
fi

# 使用sshpass和scp命令复制文件
if sshpass -p "$SSH_PASSWORD" scp "$SOURCE_FILE" "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"; then
    echo "\"${SOURCE_FILE}\" successfully copied to \"${REMOTE_PATH}\"."
    echo "Running \"${REMOTE_COMMAND}\" ..."

    # 使用sshpass和ssh命令在远程系统上执行命令
    if sshpass -p "$SSH_PASSWORD" ssh "${REMOTE_USER}@${REMOTE_HOST}" "${REMOTE_COMMAND}> /dev/null 2>&1 &"; then
        echo "\"${REMOTE_COMMAND}\" run successfully."
        exit 0
    else
        echo "Error: Failed to run \"${REMOTE_COMMAND}\"."
        exit 1
    fi
else
    echo "Error: \"${SOURCE_FILE}\" copy failed."
    exit 1
fi